<?php

namespace App\Http\Model;

use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;


class AudioApi extends Base
{

    public function __construct()
    {

    }

    //钻石赠送
    public function diamond_recharge($user_id, $diamond_num, $type, $room_id = 0)
    {
        $data['user_id'] = $user_id;
        $data['number'] = $diamond_num;
        $data['type'] = $type;
        $data['room_id'] = $room_id;
        $pearl_api = json_decode(curl_request(env('KK_API_HOST') . '/finance/diamond/decr', $data, 'urlencoded', 'GET'), true);
        if ($pearl_api['code'] !== 0) {
            //失败
            write_log('apiErrorLog', $pearl_api['msg'], $data);
        }
        return $pearl_api;
    }

    //金豆赠送
    public function pearl_recharge($user_id, $pearl_num, $msg)
    {
        $data['user_id'] = $user_id;
        $data['pearl_num'] = $pearl_num;
        $data['msg'] = $msg;
        $pearl_api = json_decode(curl_request(env('KK_API_HOST') . '/finance/pearl/recharge', $data), true);
        if ($pearl_api['code'] !== 0) {
            //失败
            write_log('apiErrorLog', 'pearlApiError', $data);
            return false;
        }
        return true;
    }


    /*
     * 用户赠送商品
     * [@param] $user_id  -- 用户id
     * [@param] $goods_id  -- 商品id
     * [@param] $goods_type  -- 商品类型
     * [@param] $time_limit  -- 赠送时间
     *
     * */
    public function sendUsedMall($user_id, $goods_id, $goods_type, $time_limit)
    {
        $data['user_id'] = $user_id;
        $data['goods_id'] = $goods_id;
        $data['goods_type'] = $goods_type;
        $data['time_limit'] = $time_limit;
        $api = json_decode(curl_request(env('KK_API_HOST') . '/mall/goods/give_user', $data), true);
        if ($api['code'] !== 0) {
            //充值失败
            write_log('sendMallError', $api['msg'], $data);
            return false;
        }
        return true;
    }


    /*
     * 发送消息通知
     * [@param] $data['user_id']  -- 用户id
     * [@param] $data['goods_id']  -- 商品id
     * */
    public function sendMsgApi($user_id, $msg)
    {
        $data['user_id'] = $user_id;
        $data['msg'] = $msg;
        $api = json_decode(curl_request(env('KK_API_HOST') . '/msg/text', $data), true);
        if ($api['code'] !== 0) {
            write_log('apiErrorLog', $api['msg'], $data);
            return false;
        }
        return true;
    }

    public function sendMsgApiUrl($user_id, $msg, $url, $action)
    {
        dump($user_id);
       return json_decode(curl_request(env('KK_API_HOST') . '/msg/text', ['user_id' => $user_id, 'msg' => $msg, 'url' => $url, 'action' => $action]), true);
    }

    /*
   * 发送全服消息通知
   * [@param] $data['user_id']  -- 用户id
   * */
    public function sendAllAppApi($user_id, $room_id, $text)
    {
        $data['user_id'] = $user_id;
        $data['room_id'] = $room_id;
        $data['text'] = $text;
        $api = json_decode(curl_request(env('KK_API_HOST') . '/activity/play_dish/full_msg', $data, 'urlencoded', 'GET'), true);
        if ($api['code'] !== 0) {
            write_log('apiErrorLog', $api['msg'], $data);
            return false;
        }
        return true;
    }

    public function registerApi($data)
    {
        return json_decode(curl_request(env('KK_API_HOST') . '/user/phone/register', $data), true);
    }


    public function sendRing($user_id, $ring_id, $time)
    {
        $data['user_id'] = $user_id;
        $data['ring_id'] = $ring_id;
        $data['time_limit'] = $time;
        $data['type'] = 1;
        $api = json_decode(curl_request(env('KK_API_HOST') . '/coupling/ring_record/create', $data, 'urlencoded', 'GET'), true);
        if ($api['code'] !== 0) {
            write_log('apiErrorLog', $api['msg'], $data);
            return false;
        }
        return true;
    }

    public function sendMedal($user_id, $medal_id, $day)
    {
        $data['user_id'] = $user_id;
        $data['medal_id'] = $medal_id;
        $data['day'] = $day;
        $api = json_decode(curl_request(env('KK_API_HOST') . '/inner/medal/user/grant', $data, 'urlencoded', 'GET'), true);
        if ($api['code'] !== 0) {
            write_log('apiErrorLog', $api['msg'], $data);
        }
        return $api;
    }

    public function sendIntimacyValue($user_id, $cp_user_id, $num)
    {
        $data['user_id'] = $user_id;
        $data['cp_user_id'] = $cp_user_id;
        $data['intimate'] = $num;
        $pearl_api = json_decode(curl_request(env('KK_API_HOST') . '/coupling/activity/intimate/incr', $data, 'urlencoded', 'GET'), true);
        if ($pearl_api['code'] !== 0) {
            //充值失败
            write_log('apiErrorLog', $pearl_api['msg'], json_encode($data));//亲密值赠送失败
            return false;
        }
        return true;
    }

    public function getCharmCharts($data)
    {
        return json_decode(curl_request(env('KK_API_HOST') . '/finance/charm_charts', $data, 'urlencoded', 'GET'), true);

    }

    public function getWealthCharts($data)
    {
        return json_decode(curl_request(env('KK_API_HOST') . '/finance/wealth_charts', $data, 'urlencoded', 'GET'), true);

    }

    //获取开启房间id
    public function getOpenRoomId()
    {
        return json_decode(curl_request(env('KK_API_HOST') . '/v2/room/online/cbd_list', ['limit' => 1000], 'urlencoded', 'GET'), true);
    }


    //发送打碟消息
    public function sendDishMsg($data)
    {
        return json_decode(curl_request(env('KK_API_HOST') . '/activity/play_dish/open_result', $data, 'urlencoded', 'GET'), true);
    }

    //
    public function openHtmlRoom($data)
    {
        return json_decode(curl_request(env('KK_API_HOST') . '/activity/year_goods_class/open_result', $data, 'urlencoded', 'GET'), true);

    }

    //发送打碟消息
    /*
     * text
     * room_id
     * user_id
     *
     * */
    public function sendRoomDishMsg($data)
    {
        return json_decode(curl_request(env('KK_API_HOST') . '/activity/play_dish/click_text', $data, 'urlencoded', 'GET'), true);
    }

    public function sendGift($user_id, $gift_id, $type, $change = 1)
    {
        $data['user_id'] = $user_id;
        $data['gift_id'] = $gift_id;
        $data['change'] = $change;//数量
        $data['platformType'] = $type;
        $send_res = json_decode(curl_request(env('KK_API_HOST') . '/inner/gift_bag/grant_gift', $data, 'urlencoded', 'GET'), true);
        if ($send_res['code'] != 0) {
            write_log('sendGiftErr', $send_res['msg'], $data);
            return false;
        }
        return true;
    }

    public function sendRoomMsg($user_id, $msg)
    {
        $data['user_id'] = $user_id;
        $data['msg'] = $msg;
        return json_decode(curl_request(env('KK_API_HOST') . '/inner/gift_bag/grant_gift', $data, 'urlencoded', 'GET'), true);
    }

    /*
     * 点亮礼物
     * @param $user_id 点亮用户id
     * @param $gift_id 点亮礼物id
     * @param $prop_id 道具卡id
     *
     * */
    public function lightsGift($user_id, $gift_id, $prop_id)
    {
        $data['user_id'] = $user_id;
        $data['gift_id'] = $gift_id;
        $data['data_id'] = $prop_id;
        return json_decode(curl_request(env('KK_API_HOST') . '/v2/inner/prop/light/up', $data, 'urlencoded', 'GET'), true);
    }

    /*
         * 通用发放奖励
         * @param $user_id 用户id
         * @param $data_id 礼物id
         * @param $prop_type 礼物类型 1钻石 2装饰 3道具 4勋章
         * @param $used_at 时间
         * @param $num 数量
         * @param $type 活动类型 9心愿卡
         *
         * */
    public function sendAllProp($user_id, $data_id, $prop_type, $num, $type, $used_at = 86400)
    {
        $data['user_id'] = $user_id;
        $data['data_id'] = $data_id;
        $data['prop_type'] = $prop_type;
        $data['num'] = $num;
        $data['type'] = $type;
        $data['used_at'] = $used_at;
        $res = json_decode(curl_request(env('KK_API_HOST') . '/prop/user_pack/get_prop', $data, 'urlencoded', 'GET'), true);
        if ($res['code'] != 0) {
            write_log('sendMallErr', $res['msg'], $data);
            return false;
        }
        return true;
    }

    public function getRoomWealth($room_id)
    {
        $data['room_id'] = $room_id;
        return json_decode(curl_request(env('KK_API_HOST') . '/v2/room/online/wealth_value', $data, 'urlencoded', 'GET'), true);

    }

    public function intimacyRank($userId, $page, $limit)
    {
        $data['user_id'] = $userId;
        $data['page'] = $page;
        $data['limit'] = $limit;
        $data['type'] = 4;
        return json_decode(curl_request(env('KK_API_HOST') . '/v2/inner/intimacy/activity_rank_charts', $data, 'urlencoded', 'GET'), true);

    }

    //修改背包礼物数量
    public function changeGiftBag($user_id, $gift_id, $num, $type = 19)
    {
        $data['user_id'] = $user_id;
        $data['gift_id'] = $gift_id;
        $data['change'] = 0 - abs($num);
        $data['platform_type'] = $type;
        return json_decode(curl_request(env('KK_API_HOST') . '/inner/gift_bag/change_gift', $data, 'urlencoded', 'GET'), true);
    }
}
